﻿// WMS Library - http://wmslib.codeplex.com/
// Copyright (c) Tom Gaskins 2004 - Erik Putrycz 2013
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WMSLib.Client;
using System.Net;
using System.Xml;
using System.Diagnostics;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using Nito.AsyncEx;
using WMSLib.WMSTools;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Threading;
using EPLabs.WeatherMaps;


namespace ULWeatherTests
{
    [TestClass]
    public class GeometTests
    {
        [TestMethod]
        public async Task TestParseWMS()
        {
            var server = new Server(@"http://geo.weather.gc.ca/geomet/");
            var r = await server.LoadData();
            var cap = server.Capabilities;
            Assert.AreEqual(167, cap.Layers.Count);
            var fLayer = cap.Layers.First();
            var layers = fLayer.Layers;
            Assert.AreEqual(6, layers.Count);
            //,
            var lat = 45.421780;
            var lon = -75.691177;
            var l = fLayer.FindBy(cl => cl.Name != null && cl.Name.StartsWith("HRDPS."), lat, lon, lat, lon);
            Assert.AreEqual(20, l.Count);
            var rain = fLayer.FindBy(cl => cl.Name != null && cl.Name.StartsWith("HRDPS.") && cl.Name.EndsWith("_PR"), lat, lon, lat, lon).Single();
            //PRECIPMM
            var styles = rain.Styles;
            var ext = new ExtendedLayer(rain, false, true);
            var uris = ext.BuildAllURLs(100, 100, new Layer.BoundingBoxType() { MinY = lat, MaxY = lat, MaxX = lon, MinX = lon }, ImageType.ImagePng);
            var dividers = ext.TimeDividers();

        }

        //RADAR_RDBR
        [TestMethod]
        public async Task TestParseWMSRadar()
        {
            var server = new Server(@"http://geo.weather.gc.ca/geomet/");
            var r = await server.LoadData();
            var cap = server.Capabilities;
            Assert.AreEqual(167, cap.Layers.Count);
            var fLayer = cap.Layers.First();
            var layers = fLayer.Layers;
            Assert.AreEqual(6, layers.Count);
            //,
            var lat = 45.421780;
            var lon = -75.691177;
            var l = fLayer.FindBy(cl => cl.Name != null && cl.Name.StartsWith("RADAR_"), lat, lon, lat, lon);
            Assert.AreEqual(4, l.Count);
            var timeRange = l.First().ParseISOInterval();
            var rain = fLayer.FindBy(cl => cl.Name != null && cl.Name.StartsWith("HRDPS.") && cl.Name.EndsWith("_PR"), lat, lon, lat, lon).Single();
            //PRECIPMM
            var style = rain.Styles.First();
            var timeRange2 = rain.ParseISOInterval();
        }

        [TestMethod]
        public async Task TestReadImage()
        {
            var svrURL = @"http://geo.weather.gc.ca/geomet/?";//HRDPS.EASTETA_RN            
            var server = new Server(svrURL);
            var f = await server.LoadData();
            var cap = server.Capabilities;
            Assert.AreEqual(167, cap.Layers.Count);
            var fLayer = cap.Layers.First();
            var layers = fLayer.Layers;
            Assert.AreEqual(6, layers.Count);

            var lat = 45.421780;
            var lon = -75.691177;
            var l = fLayer.FindBy(cl => cl.Name != null && cl.Name.StartsWith("HRDPS."), lat, lon, lat, lon);
            Assert.AreEqual(20, l.Count);
            var wind = fLayer.FindBy(cl => cl.Name != null && cl.Name.StartsWith("HRDPS.") && cl.Name.EndsWith("_UU"), lat, lon, lat, lon).Single();

            var w = 512;
            var h = 512;
            var dt = DateTime.Now;
            var hour = dt.Hour;
            if (dt.Minute > 30)
            {
                hour++;
            }
            var fDt = dt.Date;
            fDt = fDt.AddHours(hour);

            var bb = new WMSLib.Client.Layer.BoundingBoxType() { MinX = -74.6346271171326, MinY = 46.22741707027332, MaxX = -72.87694133662491, MaxY = 47.26928933489761 };
            var uris = wind.BuildAllURLs(w, h, bb, ImageType.ImagePng);
            Assert.AreEqual(25, uris.Count);
            var cts = new CancellationTokenSource();
            var b = await WMSUtils.ReadAllImages(cts.Token, new Subject<int>(), uris, async s => new Bitmap(s));
            Assert.AreEqual(25, b.Count);

        }

    }
}
